package com.songlanyun.modules.notifytemp.controller.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson.JSONObject;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.enums.WxConstant;
import com.songlanyun.common.exception.RRException;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.modules.notifytemp.model.dto.WxNotifyDTO;
import com.songlanyun.modules.rabbitmq.MqConstant;
import com.songlanyun.modules.rabbitmq.service.SendMsgService;
import com.songlanyun.modules.user.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.songlanyun.modules.notifytemp.entity.NotifyTempEntity;
import com.songlanyun.modules.notifytemp.service.NotifyTempService;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;

import javax.annotation.Resource;


/**
 * 微信消息模板表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-10 14:22:52
 */
@RestController
@RequestMapping("admin/config/notifytemp")
public class NotifyTempAdminController extends AbstractController {
    @Autowired
    private NotifyTempService notifyTempService;
    @Resource
    private SendMsgService sendMsgService;

    /**
     * 列表
     */
    @GetMapping("/list")
//    @RequiresPermissions("config:notifyTemp:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = notifyTempService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
//    @RequiresPermissions("config:notifyTemp:info")
    public R info(@PathVariable("id") Integer id) {
        NotifyTempEntity notifyTemp = notifyTempService.getById(id);

        return R.ok().put("data", notifyTemp);
    }

    /**
     * 保存
     */
    @SysLog("保存微信消息模板")
    @PostMapping("/save")
//    @RequiresPermissions("config:notifyTemp:save")
    public R save(@RequestBody NotifyTempEntity notifyTemp) {
        ValidatorUtils.validateEntity(notifyTemp);
        notifyTemp.setCreateUser(getUserId());
        notifyTempService.save(notifyTemp);

        return R.ok();
    }

    /**
     * 修改
     */
    @SysLog("修改微信消息模板")
    @PutMapping("/update")
//    @RequiresPermissions("config:notifyTemp:update")
    public R update(@RequestBody NotifyTempEntity notifyTemp) {
        ValidatorUtils.validateEntity(notifyTemp);
        notifyTempService.updateById(notifyTemp);

        return R.ok();
    }

    /**
     * 启用禁用
     */
    @SysLog("启用禁用微信消息模板")
    @PostMapping("/use")
//    @RequiresPermissions("config:notifyTemp:update")
    public R use(@RequestBody NotifyTempEntity notifyTemp) {
        Boolean isUse = notifyTemp.getIsUse();
        if (isUse == null) {
            throw new RRException("参数不能为空");
        }
        NotifyTempEntity entity = notifyTempService.getById(notifyTemp.getId());
        entity.setIsUse(isUse);
        notifyTempService.updateById(notifyTemp);

        return R.ok();
    }


    /**
     * TODO 测试通过mq发送模板消息
     */
//    @PostMapping("/send")
    public R send() {

        String openId = "123";
        List<Object> values = new ArrayList<>();
        values.add("456");

        WxNotifyDTO dto = new WxNotifyDTO(WxConstant.WxType.AFTER_THE_NEWS, openId, values);

        sendMsgService.send(MqConstant.WxNotifyConstant.WX_NOTIFY_EXCHANGE, MqConstant.WxNotifyConstant.WX_NOTIFY_ROUTER
                , JSONObject.toJSONString(dto));

        return R.ok();
    }

}
